package com.ggbz.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ggbz.entity.Students;
import com.ggbz.entity.dto.StudentUpdateRequest;
import com.ggbz.mapper.StudentsMapper;
import com.ggbz.service.StudentsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 学生信息表，存储学生的详细信息 服务实现类
 * </p>
 *
 * @author ggbz
 * @since 2025-03-08
 */
@Service
public class StudentsServiceImpl extends ServiceImpl<StudentsMapper, Students> implements StudentsService {
    @Autowired
    private StudentsMapper studentsMapper;

    @Override
    public Students getStudentById(Long userId) {
        Students student = studentsMapper.selectById(userId);
        if (student == null) {
            throw new RuntimeException("学生不存在");
        }
        return student;
    }

    @Override
    @Transactional
    public void updateStudent(Long userId,StudentUpdateRequest request) {
        Students student = studentsMapper.selectById(userId);
        UpdateWrapper<Students> wrapper = new UpdateWrapper<>();
        wrapper.eq("student_id", student.getStudentId())
                .set("class_name", request.getClassName())
                .set("major", request.getMajor())
                .set("enrollment_year", request.getEnrollmentYear());

        int updated = studentsMapper.update(null, wrapper);
        if (updated == 0) {
            throw new RuntimeException("学生不存在");
        }
    }
}
